package com.example.feifei.fragment;

import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.feifei.newproject.R;
import com.example.feifei.utils.LogUtils;
import com.example.feifei.utils.ToastUtil;

import java.io.File;
import java.io.IOException;

/**
 * 附近Fragment
 * Created by feifei on 2017/12/4.
 */

public class NearByFrgment extends Fragment implements View.OnClickListener {

    private static final String LOG_TAG = "AudioRecordTest";

    private View root_view;

    private Button btn_startVoice;
    private Button btn_stopVoice;
    private Button start_play;
    private Button stop_play;

    private static final int recode_audio = 9;

    //语音文件保存路径
    private String FileName = null;
    //语音操作对象
    private MediaPlayer mPlayer = null;
    private MediaRecorder mRecorder = null;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root_view = inflater.inflate(R.layout.view_custom_content, null);
        initView();

        return root_view;
    }

    private void initView() {
        btn_startVoice = (Button) root_view.findViewById(R.id.btn_startVoice);
        btn_startVoice.setOnClickListener(this);
        btn_stopVoice = (Button) root_view.findViewById(R.id.btn_stopVoice);
        btn_stopVoice.setOnClickListener(this);
        start_play = (Button) root_view.findViewById(R.id.start_play);
        start_play.setOnClickListener(this);
        stop_play = (Button) root_view.findViewById(R.id.stop_play);
        stop_play.setOnClickListener(this);
        FileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/amyvoice/";
//        FileName = "file:///sdcard/myvido/a.3pg";
        File file=new File(FileName,"a.aac");
        try{
            if(file.exists()){
                file.delete();
                file.createNewFile();
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //调用录音
            case R.id.btn_startVoice:
                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
                mRecorder.setOutputFile(FileName);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

                try {
                    mRecorder.prepare();
                    mRecorder.start();
                    LogUtils.e("开始录音");
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    LogUtils.e("IllegalStateException"+e.getMessage().toString());
                } catch (IOException e) {
                    e.printStackTrace();
                    LogUtils.e("IO"+e.getMessage().toString());
                }
                ToastUtil.showMessage(getContext(), "开始录音");

                break;
            //结束录音
            case R.id.btn_stopVoice:
                if (mRecorder != null) {
                    try {
                        mRecorder.stop();

                    } catch (IllegalStateException e) {
                        // TODO 如果当前java状态和jni里面的状态不一致，
                        //e.printStackTrace();
                        mRecorder = null;
                        mRecorder = new MediaRecorder();
                    }
                    mRecorder.release();
                    mRecorder = null;
                    ToastUtil.showMessage(getContext(), "结束录音");
                }
                break;
            case R.id.start_play:
                mPlayer = new MediaPlayer();
                try {
                    mPlayer.setDataSource(FileName);
                    mPlayer.prepare();
                    mPlayer.start();
                } catch (IOException e) {
                    LogUtils.e(LOG_TAG, "播放失败");
                }

                ToastUtil.showMessage(getContext(), "开始播放");
                break;
            //结束播放
            case R.id.stop_play:
                // TODO Auto-generated method stub
                if (mRecorder != null) {
                    try {
                        mRecorder.stop();
                    } catch (IllegalStateException e) {
                        // TODO 如果当前java状态和jni里面的状态不一致，
                        //e.printStackTrace();
                        mRecorder = null;
                        mRecorder = new MediaRecorder();
                        mRecorder.stop();
                    }
                    mRecorder.release();
                    mRecorder = null;
                }
                ToastUtil.showMessage(getContext(), "结束播放");
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case recode_audio:
                ToastUtil.showMessage(getContext(), "听到录音");
                break;
        }
    }
}
